﻿using DevExpress.XtraEditors.Repository;
using DevTeam;

using System.Data;

namespace ConfExpert.Forms.ImportWizards
{
    public partial class XIPL : XImportWizard
    {
        public XIPL()
        {
            InitializeComponent();

            this.ProcessStart += XITopCekiListesi_ProcessStart;
            this.ProcessDone += XITopCekiListesi_ProcessDone;
            btnSave.ItemClick += BtnSave_ItemClick;
        }

        public DataTable dtBoth;
        public DataTable dtBD = new DataTable("BD");
        private void BtnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DataSet ds = MasterGrid.Dataset();
            if (ds.Tables[0].Select("ProductId is null").Length > 0)
            {
                ShowErrorMessage("ProductId lerin tamamı dolu olmalı!");
                return;
            }

            //string str1 = ds.Tables[0].Serialize();
            //string str2 = ds.Tables[1].Serialize();

            //int res = ExecuteScalar("UpdYuklemeTopBilgileri2", /*YuklemeId, /*idxs,*/ str1, str2).ToInt();       //DİKKAT!
            //if (res > 0)
            //{
            //    Notify("Yüklemeye ilişkin top bilgileri sisteme aktarıldı.");
            //    this.DialogResult = System.Windows.Forms.DialogResult.OK;
            //}
            //else
            //    ShowErrorMessage("Yükleme ve dosyadaki artikeller uyuşmuyor!");

        }

        void XITopCekiListesi_ProcessDone()
        {
            /*
            var distinctValues = Article.AsEnumerable().Select(row => new { Kod = row.Field<string>("Article"), }).Distinct().ToList();
            string codes = string.Join(",", Array.ConvertAll(distinctValues.ToArray(), i => i.Kod));
            
            DataTable dtId = Session.DataManager.ExecuteDataTable("GetProductIdByCode", codes);
            foreach (DataRow drArticle in Article.Rows)
            {
                DataRow[] drIds = dtId.Select("Kod='" + drArticle["Article"] + "'");
                if (drIds.Length == 1)
                    drArticle["ProductId"] = (int)drIds[0]["ProductId"];
            }
        */

            /*
             * 
            DataTable dtId = AppSession.DataManager.ExecuteDataTable("GetProductIdByCode2", codes);
            foreach (DataRow drArticle in Article.Rows)
            {
                DataRow[] drIds = dtId.Select("Kod='" + drArticle["Article"] + "'");
                if (drIds.Length == 1)
                    drArticle["ProductId"] = (int)drIds[0]["ProductId"];
            }
             * 
             * DESTEX_AF_1 için!
             */

            MasterGrid.Init(dtBoth);
            //MasterGrid.AddDetailGrid(DetailGrid, "PL");
            //MasterGrid.SutunEkle("TotalQty", typeof(decimal), "SUM(Child({FK}).Qty)");
            //MasterGrid.SutunEkle("RollCount", typeof(int), "COUNT(Child({FK}).Article)");
            //DetailGrid.MakeColumnInvisible("Article");
            //MasterGrid.YanYanaGetir("TotalQty:CustomerCode");

            MasterGrid.AddSummary("xTotal", "SipQty", "KesQty");
            //DetailGrid.AddSummary();

            MasterGrid.MakeReadOnly();
            //DetailGrid.MakeReadOnly();

            MasterGrid.SetViewCaption("Packing List");
            //DetailGrid.SetViewCaption("Toplar");

            //DetailGrid.Columns["Qty"].Width = 100;
            //DetailGrid.BestFitColumns();
            MasterGrid.BestFitColumns();

            //Kontrol();

        }

        void XITopCekiListesi_ProcessStart()
        {
            Result = new DataSet();
            Article = new DataTable();
            Result.Tables.Add(Article);

            Article.Columns.Add("SheetName", typeof(string));
            Article.Columns.Add("DosyaNo", typeof(string));
            Article.Columns.Add("OrderNr", typeof(string));
            Article.Columns.Add("BigBoxNo", typeof(int));
            Article.Columns.Add("BoxNo", typeof(int));
            Article.Columns.Add("NrRange", typeof(string));
            Article.Columns.Add("Model", typeof(string));
            Article.Columns.Add("Artikel", typeof(string));
            Article.Columns.Add("Renk", typeof(string));
            Article.Columns.Add("Magaza", typeof(string));
            Article.Columns.Add("Brand", typeof(string));
            Article.Columns.Add("City", typeof(string));
            
            Article.Columns.Add("Modello", typeof(string)); //GP
            Article.Columns.Add("FaturaNo", typeof(string));    //GP

            string col = string.Empty;
            for (int i = 1; i < 29; i++)
            {
                Article.Columns.Add(string.Format("A{0:00}", i), typeof(int));
                col += string.Format("ISNULL(A{0:00},0)+", i);
            }
            Article.Columns.Add("Total", typeof(int), col + "0");

            col = string.Empty; 
            for (int i = 1; i < 50; i++)
            {
                Article.Columns.Add(("x" + i), typeof(int));
                col += string.Format("ISNULL(x" + i + ",0)+ ");
            }
            Article.Columns.Add("xTotal", typeof(int), col + "0");
        }


        public DataTable Article;
        public DataSet Result;

        private void XIPL_Load(object sender, System.EventArgs e)
        {
            if (!DesignMode)
            {
                ((RepositoryItemGridLookUpEdit)gleSezon.Edit).Assign(RI(Rp.Sezon));
                ((RepositoryItemGridLookUpEdit)gleCKBatch.Edit).Assign(RI(Rp.CKBatch));
            }

            if (dtBD.Columns.Count == 0)
            {
                dtBD.Columns.Add("SheetName", typeof(string));
                dtBD.Columns.Add("Caption", typeof(string));
                dtBD.Columns.Add("Beden", typeof(string));
                dtBD.Columns.Add("Name", typeof(string));
                dtBD.Columns.Add("Sira", typeof(string));
            }
        }
        /*
        public void Kontrol()
        {
            bool res = false;
            DataTable Prd = new DataTable();

            DataSet d = ExecuteDataSet("CheckTopCeki", YuklemeId, Article.Serialize());
            if (d.Tables.Count > 0)
            {
                btnSave.Enabled = false;

                using (ArtikelMetrajEsleme popup = new ArtikelMetrajEsleme(d))
                    if (popup.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        Prd = popup.d.Tables[1];
                        btnSave.Enabled = true;
                        res = true;
                    }
            }

            if (res)
            {
                foreach (DataRow r in Prd.Rows)
                    MasterGrid.DataTable().Select("Article='" + r["Article"] + "'")[0]["ProductId"] = r["ProductId"];

                MasterGrid.RefreshData();
            }
        }
         */
    }


}
